<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2023/3/13
 * Time: 10:18
 */

namespace App\Rep;


use App\Models\Customer;
use App\Models\Rbac\User;
use App\Models\Rbac\UserRole;
use App\Rep\Rbac\UserRep;
use Illuminate\Support\Facades\DB;

class CustomerRep
{
    /**
     * 获取客户列表
     * @param $params
     * @return mixed
     */
    public function getCustomerListRep($params){
        $list = Customer::Search($params)->orderBy('id','desc')->paginate($params['per_page']??1000);
        return $list;
    }

    /**
     * 添加客户
     * @param $params
     * @return Customer|array
     */
    public function createCustomerRep($params){
        if(isset($params['id']) && !empty($params['id'])){
            $model = Customer::where('id',$params['id'])->first();
        }else{
            $model = new Customer();
        }
        $model->customer_name = $params['customer_name'];
        $model->provinces = $params['provinces']??"";
        $model->city = $params['city']??'';
        $model->county = $params['county']??'';
        $model->customer_address = $params['customer_address']??'';
        $model->contact = $params['contact']??"";
        $model->contact_phone = $params['contact_phone']??"";
        $model->remark = $params['remark']??'';
        if(isset($params['balance'])){
            $model->balance = $params['balance'];
        }
        $model->store_name = $params['store_name']??'';
        $model->save();
        return ['code'=>0,'data'=>$model,'msg'=>'success'];
    }

    /**
     * 删除客户
     * @param $params
     */
    public function delCustomerRep($params){
        $model = Customer::where('id',$params['id'])->first();
        $model->is_del = $params['is_del'];
        $model->save();
        return $model;
    }

    /**
     * 充值
     * @param $params
     */
    public function topUpRep($params){
        DB::beginTransaction();
        try{
            $model = Customer::where('id',$params['id'])->first();
            //添加资金变动记录
            $balanceHistoryRep = new BalanceHistoryRep();
            $balanceHistoryRep->addBalanceHistoryRep($params['id'],$params['balance'],$model->balance,$model->balance + $params['balance'],0,'余额充值',$detail='');

            $model->balance = $model->balance + $params['balance'];
            $model->save();
            DB::commit();
            return ['code'=>0,'data'=>$model,'msg'=>'success'];
        }catch(\Exception $exception){
            DB::rollBack();
            return ['code'=>2001,'data'=>[],'msg'=>$exception->getMessage()];
        }

    }

    /**
     * 获取经销商列表
     * @param $params
     * @return void
     */
    public function getDealerListRep($params){
        $role_id = 18;
        $user_id_arr = UserRole::where('role_id',$role_id)->pluck('user_id')->toArray();
        $list = User::whereIn('id',$user_id_arr)
            ->where('is_del',2)
            ->with(['dealer'])
            ->orderBy('id','desc')
            ->paginate($params['per_page']??1000);
        return $list;
    }


    /**
     * 添加，编辑经销商
     * @param $params
     * @return Customer|array
     */
    public function createDealerRep($params){
        ////p($params);
        DB::beginTransaction();
        try{
            if(isset($params['id']) && !empty($params['id'])){
                $model = Customer::where('id',$params['id'])->first();
            }else{
                $model = new Customer();
                $userRep = new UserRep();
                $user = User::where('username',$params['usernames'])->first();
                $user_id = '';
                if($user){
                    $user_id = $user->id;
                }
                $user_data = [
                    'usernames'=>$params['usernames']??'',
                    'passwords'=>$params['passwords']??88888888,
                    'phones'=>$params['phones']??'',
                    'company_names'=>$params['customer_name']??'',
                    'parent_username'=>$params['parent_username'],
                    'roles_id'=>18,
                    'id'=>$user_id
                ];
                $userObj = $userRep->createUserRep($user_data);
                //p($userObj);
                $model->user_id = $userObj['data']->id;
            }
            //p($params);
            $model->customer_name = $params['customer_name'];//客户名称
            $model->provinces = $params['provinces']??"";
            $model->city = $params['city']??'';
            $model->county = $params['county']??'';
            $model->customer_address = $params['customer_address']??'';
            $model->contact = $params['contact']??"";//联系人
            $model->contact_phone = $params['contact_phone']??"";
            $model->remark = $params['remark']??'';
            if(isset($params['balance'])){
                $model->balance = $params['balance'];
            }
            $model->store_name = $params['store_name']??'';
            $model->save();
            //p($model);
            DB::commit();
            return ['code'=>0,'data'=>$model,'msg'=>'success'];
        }catch(\Exception $exception){
            DB::rollBack();
            return ['code'=>2001,'data'=>[],'msg'=>$exception->getMessage()];
        }

    }


}
